package com.songmao.pricecloudapitest.testcase.usermanagement;

import com.songmao.pricecloudapitest.requestcore.ResponePoJo;
import com.songmao.pricecloudapitest.server.PriceCloudHttpCore;
import com.songmao.pricecloudapitest.utils.AssertCore;
import com.songmao.pricecloudapitest.utils.ParsingJson;
import com.songmao.pricecloudapitest.utils.TestData;
import com.songmao.pricecloudapitest.utils.TestUitl;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Remy
 * @description : 客户管理,搜索相关测试用例
 * @date 2020-07-08 15:34
 */
public class SelectUserTestCase extends PriceCloudHttpCore {

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据客户代号进行搜索")
    public void selectCustomerCodeTestCase(Map<String, String> map) {
        ResponePoJo pojo = super.get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        String actual =
                Objects.requireNonNull(ParsingJson.getPathCode(pojo.getData().toString(), map.get("expectedPath"))).toString();
        AssertCore.assertNotNull(actual, "实际值为空");
        AssertCore.assertEquals(map.get("expected"), actual);

        if (map.get("businessDependence") != null) {
            String appid = Objects.requireNonNull(ParsingJson.getPathCode(pojo.getData().toString(), map.get("businessDependence"))).toString();
            TestUitl.setAttribute("appid", appid);
        }
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据错误的报价用户进行搜索")
    public void failureSelectOfferUsers(Map<String, String> map) {
        ResponePoJo pojo = super.get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        String actual = Objects.requireNonNull(ParsingJson.getPathCode(pojo.getData().toString(), map.get("expectedPath"))).toString();
        AssertCore.assertNotNull(actual, "实际值为空");
        AssertCore.assertEquals(map.get("expected"), actual);
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据错误的邮箱进行搜索")
    public void failureSelectEmailUsers(Map<String, String> map) {
        ResponePoJo pojo = super.get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        String actual = Objects.requireNonNull(ParsingJson.getPathCode(pojo.getData().toString(), map.get("expectedPath"))).toString();
        AssertCore.assertNotNull(actual, "实际值为空");
        AssertCore.assertEquals(map.get("expected"), actual);
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据appid进行搜索")
    public void selectAppidUsers(Map<String, String> map) {
        ResponePoJo pojo = super.get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        String actual = Objects.requireNonNull(ParsingJson.getPathCode(pojo.getData().toString(), map.get("expectedPath"))).toString();
        AssertCore.assertEquals(map.get("expected"), actual);
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据正确的报价用户进行搜索")
    public void successSelectOfferUsers(Map<String, String> map) {
        Map<String, String> expectedPath = TestUitl.splitString(map.get("expectedPath"));
        ResponePoJo pojo = super.get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        assert expectedPath != null;
        String appid = (String) ParsingJson.getPathCode(pojo.getData().toString(), expectedPath.get("appid"));

        String url = TestUitl.replaceAccessToken(expectedPath.get("url"), "#{appid}", appid);
        ResponePoJo rs = get(url);
        List<String> accountList = (List<String>) (List) ParsingJson.getPathCodes(rs.getData().toString(), expectedPath.get("account"));
        assert accountList != null;
        AssertCore.assertTrue(accountList.contains(map.get("expected")));

    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据正确的邮箱进行搜索")
    public void successSelectEmailUsers(Map<String, String> map) {
        Map<String, String> expectedPath = TestUitl.splitString(map.get("expectedPath"));

        ResponePoJo pojo = super.get(map.get("url"));
        AssertCore.assertEquals(200, pojo.getStatus());
        assert expectedPath != null;
        String appid = (String) ParsingJson.getPathCode(pojo.getData().toString(), expectedPath.get("appid"));

        String url = TestUitl.replaceAccessToken(expectedPath.get("url"), "#{appid}", appid);
        ResponePoJo rs = get(url);
        List<String> accountList = (List<String>) (List) ParsingJson.getPathCodes(rs.getData().toString(), expectedPath.get("uemail"));
        assert accountList != null;
        AssertCore.assertTrue(accountList.contains(map.get("expected")));
    }

}
